﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class FundsOperatingDetailsReportController : Controller
    {
        private FundsOperatingDetailsReportInfo fundinfo = new FundsOperatingDetailsReportInfo(true);
        public ActionResult FundsOperatingDetailsReportIndex()
        {
            ViewData["Deptment"] = GetDeptment();
            ViewData["GroupDept"] = GetGroupDept("-1");
            ViewData["DepositTypeID"] = GetDepositTypeID();
            return View();
        }

        /// <summary>
        /// 获取增款名称列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDepositTypeID()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            DataSet ds = fundinfo.GetDepositTypeID();
            dic.Add("-1", "请选择...");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(ds.Tables[0].Rows[i]["DepositName"].ToString().Trim(), ds.Tables[0].Rows[i]["DepositName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取班组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroupDept(string depid)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = DeptDDLInfo.GetGroupDept(depid);
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["groupid"]), ds.Tables[0].Rows[i]["groupname"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDeptment()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = fundinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetGroupDeptChange(string depid)
        {
            ViewData["GroupDept"] = GetGroupDept(depid);
            return Json(ViewData["GroupDept"]);
        }

        public ActionResult GetList(PageVM pvm)
        {
            string condition = pvm.query;
            string starttime = condition.Split(',')[0].ToString();
            string endtime = condition.Split(',')[1].ToString();
            int depid = Convert.ToInt32(condition.Split(',')[2]);
            string empno = condition.Split(',')[3].ToString();
            string pname = condition.Split(',')[4].ToString();
            int groupid = Convert.ToInt32(condition.Split(',')[5]);
            string strdeptid = " and 1=1 ";
            string strGroup_ID = " and 1=1 ";
            if (depid != -1)
            {
                strdeptid = " and DEP_ID=" + depid;
            }
            if (groupid != -1)
            {
                strGroup_ID = " and GroupIdDept=" + groupid;
            }
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = fundinfo.GetFundsOperatingDetailsReportCountWhere(" and RDATETIME>='" + starttime + "' and RDATETIME<='" + endtime + "'" + strdeptid + strGroup_ID + " and EMPNO like'%" + empno + "%' and PNAME like'%" + pname + "%'");

            DataSet list = fundinfo.BindList(pvm.rp, pvm.page, " and RDATETIME>='" + starttime + "' and RDATETIME<='" + endtime + "'" + strdeptid + strGroup_ID + " and EMPNO like'%" + empno + "%' and PNAME like'%" + pname + "%'");

            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                #region
                v = new List<string>();
                v.Add(item["ID"].ToString());
                v.Add(item["RDATETIME"].ToString());
                v.Add(item["DEPTNAME"].ToString());
                v.Add(item["EMPNO"].ToString());
                v.Add(item["PNAME"].ToString());
                v.Add(item["CONMONEY"].ToString());
                v.Add(item["DEALTYPE"].ToString());
                v.Add(item["DEPOSITNAME"].ToString());
                v.Add(item["BALANCE"].ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item[0].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 食堂零星物品领用表
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string StartTime, string EndTime, string DEP_ID, string EMPNO, string PNAME, string GroupId)
        {
            return EContent(StartTime, EndTime, DEP_ID, EMPNO, PNAME, GroupId);
        }

        public ExcelContentResult EContent(string startTime, string endTime, string depid, string empno, string pname, string groupid)
        {
            string starttime = startTime.ToString();
            string endtime = endTime.ToString();
            string depid1 = depid;
            string empno1 = empno.ToString();
            string pname1 = pname.ToString();
            string groupid1 = groupid;
            string strdeptid = " and 1=1 ";
            string strgroupid = " and 1=1 ";
            if (depid1 != "-1")
            {
                strdeptid = " and DEP_ID=" + depid;
            }
            if (groupid1 != "-1")
            {
                strgroupid = " and GroupIdDept=" + groupid1;
            }
            StringBuilder sBuilder = new StringBuilder();
            //int totlerows = fundinfo.GetFundsOperatingDetailsReportCountWhere("");
            DataSet list = fundinfo.GetListAll(" and RDATETIME>='" + starttime + "' and RDATETIME<='" + endtime + "'" + strdeptid + strgroupid + " and EMPNO like'%" + empno1 + "%' and PNAME like'%" + pname1 + "%'");
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='8' style='font-size:25px; text-align:center;'>增减款明细报表</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            //sBuilder.Append("<td>ID</td>");
            //sBuilder.Append("<td>零星物品编号</td>");
            sBuilder.Append("<td>日期时间</td>");
            sBuilder.Append("<td>部门名称</td>");
            sBuilder.Append("<td>人员编号</td>");
            sBuilder.Append("<td>姓名</td>");
            sBuilder.Append("<td>金额</td>");
            sBuilder.Append("<td>增项减项</td>");
            sBuilder.Append("<td>增减名称</td>");
            sBuilder.Append("<td>操作后余额</td>");
            sBuilder.Append("</tr>");
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px;white-space: nowrap;'>");
                sBuilder.Append("<td>" + item["RDATETIME"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["DEPTNAME"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["EMPNO"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["PNAME"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["CONMONEY"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + item["DEALTYPE"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["DEPOSITNAME"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["BALANCE"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "zenjianmingxi";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
